package org.dromara.system.listener;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.excel.core.ExcelListener;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.system.domain.WbCompanyInfo;
import org.dromara.system.domain.bo.WbCompanyInfoBo;
import org.dromara.system.domain.bo.WbGoodsListInfoBo;
import org.dromara.system.domain.bo.WbMinuteBidBagBo;
import org.dromara.system.domain.bo.WbMinuteListBo;
import org.dromara.system.domain.vo.WbMinuteBidBagHistoryVo;
import org.dromara.system.domain.vo.WbMinuteBidBagVo;
import org.dromara.system.service.IWbCompanyInfoService;
import org.dromara.system.service.IWbMinuteBidBagService;
import org.dromara.system.service.IWbMinuteListService;
import org.dromara.system.service.IWbProvinceService;

import java.math.BigDecimal;
import java.util.List;

/**
 * 系统用户自定义导入
 *
 * @author Lion Li
 */
@Slf4j
public class WbMinuteBidBagHistoryImportListener extends AnalysisEventListener<WbMinuteBidBagHistoryVo> implements ExcelListener<WbMinuteBidBagHistoryVo> {

    private final IWbMinuteBidBagService wbMinuteBidBagService;

    private final IWbMinuteListService wbMinuteListService;

    private final IWbProvinceService wbProvinceService;

    private final IWbCompanyInfoService wbCompanyInfoService;

    private int successNum = 0;
    private int failureNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public WbMinuteBidBagHistoryImportListener() {
        this.wbMinuteBidBagService = SpringUtils.getBean(IWbMinuteBidBagService.class);
        this.wbMinuteListService = SpringUtils.getBean(IWbMinuteListService.class);
        this.wbProvinceService = SpringUtils.getBean(IWbProvinceService.class);
        this.wbCompanyInfoService = SpringUtils.getBean(IWbCompanyInfoService.class);
    }

    @Override
    public void invoke(WbMinuteBidBagHistoryVo wbMinuteBidBagHistoryVo, AnalysisContext context) {
        try {
            if(StringUtils.isNotEmpty(wbMinuteBidBagHistoryVo.getItemName())){
                WbMinuteBidBagBo wbMinuteBidBagBo = new WbMinuteBidBagBo();
                wbMinuteBidBagBo.setItemName(wbMinuteBidBagHistoryVo.getItemName());
                List<WbMinuteBidBagVo> list_wbMinuteBidBagVo = wbMinuteBidBagService.queryListByName(wbMinuteBidBagBo);
                if(list_wbMinuteBidBagVo.size() == 0){
                    wbMinuteBidBagBo.setYear(wbMinuteBidBagHistoryVo.getYear());
                    Long id =  wbProvinceService.queryByName(wbMinuteBidBagHistoryVo.getProvince());
                    if(id != null){
                        wbMinuteBidBagBo.setProvince(id.toString());
                    }
                    wbMinuteBidBagBo.setStatus("0");
                    wbMinuteBidBagService.insertByBo(wbMinuteBidBagBo);
                }else{
                    wbMinuteBidBagBo = BeanUtil.toBean(list_wbMinuteBidBagVo.get(0), WbMinuteBidBagBo.class);
                }
                WbMinuteListBo wbMinuteListBo = new WbMinuteListBo();
                wbMinuteListBo.setWbMinuteBidBagId(wbMinuteBidBagBo.getId());
                wbMinuteListBo.setSubStandardName(wbMinuteBidBagHistoryVo.getSubStandardName());
                wbMinuteListBo.setBagNo(wbMinuteBidBagHistoryVo.getBagNo());
                if(StringUtils.isNotEmpty(wbMinuteBidBagHistoryVo.getPrice()) && isValidBigDecimal(wbMinuteBidBagHistoryVo.getPrice())){
                    wbMinuteListBo.setPrice(wbMinuteBidBagHistoryVo.getPrice());
                }else{
                    wbMinuteListBo.setPriceDescription(wbMinuteBidBagHistoryVo.getPrice());
                }
                wbMinuteListBo.setCompanyName(wbMinuteBidBagHistoryVo.getCompanyName().replaceAll(" ", ""));
                wbMinuteListService.insertByBo(wbMinuteListBo);
                if(StringUtils.isNotEmpty(wbMinuteListBo.getCompanyName())){
                    List<WbCompanyInfo> list_wbCompanyInfo =  wbCompanyInfoService.queryByName(wbMinuteListBo.getCompanyName());
                    if(list_wbCompanyInfo.size() == 0){
                        WbCompanyInfoBo wbCompanyInfoBo = new WbCompanyInfoBo();
                        wbCompanyInfoBo.setName(wbMinuteListBo.getCompanyName());
                        wbCompanyInfoBo.setStatus("0");
                        wbCompanyInfoService.insertByBo(wbCompanyInfoBo);
                    }
                }
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、项目编号 ").append(wbMinuteBidBagHistoryVo.getItemName()).append(" 导入成功");
            }else{
                failureNum++;
                String msg = "<br/>" + failureNum + "、无(name) 导入失败：";
                failureMsg.append(msg).append("无(name)");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、项目名称 " + wbMinuteBidBagHistoryVo.getItemName() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    public static boolean isValidBigDecimal(String str) {
        try {
            new BigDecimal(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    @Override
    public ExcelResult<WbMinuteBidBagHistoryVo> getExcelResult() {
        return new ExcelResult<>() {

            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<WbMinuteBidBagHistoryVo> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }
}
